<?php

/**
 * as51 酒店预订搜索
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: zhaohong $
 * $Id: search_hotel.php 17067 2010-03-26 03:59:37Z liuhui $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
$smarty->assign('guid','search_hotel');

//城市区列表
$hotel_city_list=get_hotel_city_list();
$smarty->assign('hotel_city_list',$hotel_city_list);

//右边数据
//热门城市
$srh_data=array();
$srh_data['is_hot']=1;
$hotel_city_list_hot=get_hotel_city_list($srh_data);
$smarty->assign('hotel_city_list_hot',$hotel_city_list_hot);
//热门酒店
$srh_data=array();
$srh_data['is_hot']=1;
$hotel_list_hot=get_hotel_list($srh_data,'','','booking_times','desc',1);
$smarty->assign('hotel_list_hot',$hotel_list_hot['list']);
//特色酒店
$srh_data=array();
$srh_data['is_best']=1;
$hotel_list_best=get_hotel_list($srh_data);
$smarty->assign('hotel_list_best',$hotel_list_best['list']);
//酒店排行
$hotel_list_sell=get_hotel_list('',10,1,'booking_times','desc');
$smarty->assign('hotel_list_sell',$hotel_list_sell['list']);
//右边数据 end

//搜索参数
$srch_param=array();//调用函数的搜索参数

//处理搜索关键字传值数据
$key_data=$_REQUEST;//传回页面记录搜索条件用
$smarty->assign('key_data',$key_data);
$srch_data=$_REQUEST;//sql搜索用
$key_pager_request=$_REQUEST;//翻页用



@$srch_data[$key]=get_html_val($srch_data);//处理搜索参数

foreach ($key_pager_request as $key=>$val){
	@$key_pager_request[$key]=urlencode(trim($val));
}
//print_pre_r($srch_data);echo $cache_id_param;exit;

//提取有效搜索参数

if(!empty($srch_data['city_name_cn'])){$srch_param['city_name_cn']=$srch_data['city_name_cn'];}
if(!empty($srch_data['city_id'])){$srch_param['city_id']=$srch_data['city_id'];}
if(!empty($srch_data['area_id'])){$srch_param['area_id']=$srch_data['area_id'];}
if(!empty($srch_data['biz_area_id'])){$srch_param['biz_area_id']=$srch_data['biz_area_id'];}
if(!empty($srch_data['hotel_star'])){$srch_param['hotel_star']=$srch_data['hotel_star'];}
if(!empty($srch_data['city_name'])){$srch_param['city_name']=$srch_data['city_name'];}
if(!empty($srch_data['from_time'])){$srch_param['from_time']=$srch_data['from_time'];}
if(!empty($srch_data['to_time'])){$srch_param['to_time']=$srch_data['to_time'];}
if(!empty($srch_data['shop_price'])){$srch_param['shop_price']=$srch_data['shop_price'];}
if(!empty($srch_data['room_price_max'])){$srch_param['room_price_max']=$srch_data['room_price_max'];}
if(!empty($srch_data['room_price_min'])){$srch_param['room_price_min']=$srch_data['room_price_min'];}
if(!empty($srch_data['hotel_name'])){$srch_param['hotel_name']=$srch_data['hotel_name'];}
if(!empty($srch_data['hotel_star'])){$srch_param['hotel_star']=$srch_data['hotel_star'];}
if(!empty($srch_data['near_txt'])){$srch_param['near_txt']=$srch_data['near_txt'];}
if(!empty($srch_data['establishment'])){$srch_param['establishment']=$srch_data['establishment'];}

$cache_id_param='';//缓存页id部分
if(!empty($srch_param)){
	//执行搜索
	/* 初始化分页信息 */
	$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
	$size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
	
	$filter_attr_str = isset($_REQUEST['filter_attr']) ? trim($_REQUEST['filter_attr']) : '0';
	$filter_attr_str = urldecode($filter_attr_str);
	$filter_attr = empty($filter_attr_str) ? '' : explode('.', trim($filter_attr_str));

	/* 排序、显示方式以及类型 */
	$default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'text');
	$default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
	$default_sort_order_type   = $_CFG['sort_order_type'] == '0' ? 'booking_times' : ($_CFG['sort_order_type'] == '1' ? 'goods_name' : 'last_update');
	$sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('booking_times', 'goods_name'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;
	$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))? trim($_REQUEST['order']) : $default_sort_order_method;
	$display  = (isset($_REQUEST['display']) && in_array(trim(strtolower($_REQUEST['display'])), array('list', 'grid', 'text'))) ? trim($_REQUEST['display'])  : (isset($_COOKIE['ECS']['display']) ? $_COOKIE['ECS']['display'] : $default_display_type);

	$pager['search']=array();//分页链接参数
	if(!empty($srch_param)){
		foreach ($srch_param as $key=>$val){
			if($val!=''){
				$pager['search'][$key]=$val;
			    $cache_id_param.=$key.'-';
			}else{
				unset($srch_param[$key]);
			}
		}
	}
	$srch_param['from_time']=empty($srch_param['from_time'])?'':strtotime($srch_param['from_time']);
    $srch_param['to_time']=empty($srch_param['to_time'])?'':strtotime($srch_param['to_time']);
}

/* 页面的缓存ID */
$cache_id = sprintf('%X', crc32($cache_id_param . $display . '-' . $sort  .'-' . $order  .'-' . $page . '-' . $size . '-' . $_SESSION['user_rank'] . '-' .
$_CFG['lang'] .'-'. $filter_attr_str));

if (!$smarty->is_cached('search_hotel.dwt', $cache_id))
{
	/* 如果页面没有被缓存则重新获取页面的内容 */
	if(empty($srch_param)){
		//刚进入搜索
		$smarty->display('search_hotel.dwt',$cache_id);
		die();
	}else{
		//判断是否只搜索某个城市/区并匹配已有的城市
		if(!empty($hotel_city_list)){
			foreach ($hotel_city_list as $key=>$val){
				//处理参数值
				if(ereg('[a-zA-Z]',$srch_param['city_name'])){
					$srch_param['city_name']=strtolower($srch_param['city_name']);
				}
				$val['city_name_cn']=strtolower($val['city_name_cn']);
                //开始判断
				if(empty($srch_param['city_name'])&&empty($srch_param['city_name_cn'])&&empty($srch_param['hotel_name'])&&empty($srch_param['near_txt'])
				        &&empty($srch_param['city_id'])&&empty($srch_param['area_id'])&&empty($srch_param['biz_area_id'])){
					//如果没指定某个城市则自动取ip地址所在城市
					$ip_place=get_ip_place();
					$srch_param['city_name']=$ip_place[3];
					if(empty($city_key)){
						$srch_param['city_name']=='广州';//自动获取失败则默认是广州
					}
				}
				if($val['city_name']==$srch_param['city_name']||$val['city_name_cn']==$srch_param['city_name']||@strstr($val['city_name'],$srch_param['city_name'])||@strstr($val['city_name_cn'],$srch_param['city_name'])){
					
					//$srch_param=array();//清空其他参数
					$srch_param['hc_id']=$val['hc_id'];
					$to_city_inf=$val;//匹配的城市
					break;
				}
			}
		}
		//执行搜索
		if(!empty($srch_param)){
			/*
			<option value="" selected="selected">不限</option>
			<option  value="5">700以上</option>
			<option  value="4">500-700</option>
			<option  value="3">300-500</option>
			<option  value="2">200-300</option>
			<option  value="1">200以下</option>
			*/
			switch ($srch_param['shop_price']){
				case 1:
					//$srch_param['room_price_min']=0;
					$srch_param['room_price_max']=200;
					break;
				case 2:
					$srch_param['room_price_min']=200;
					$srch_param['room_price_max']=300;
					break;
				case 3:
					$srch_param['room_price_min']=300;
					$srch_param['room_price_max']=500;
					break;
				case 4:
					$srch_param['room_price_min']=500;
					$srch_param['room_price_max']=700;
					break;
				case 5:
					$srch_param['room_price_min']=700;
					break;
			}
			unset($srch_param['shop_price']);
			$srch_param['establishment']=empty($srch_param['establishment'])?'':implode('%',$srch_param['establishment']);
			if(!empty($srch_param)){
				$search_data_list_arr=get_hotel_list($srch_param,$size,$page,$sort,$order);//执行搜索
			}
		}
		//处理搜索结果
		$search_data_list=$search_data_list_arr['list'];
		$count=$search_data_list_arr['count'];
		$hotel_inf=get_first_ele($search_data_list);//第一条记录
		if($count==1){
			//如果只有一个酒店，则直接进入该酒店
			hy_header('booking_hotel.php?hotel_id='.$hotel_inf['goods_id']);
		}

		//返回搜索form的值
		$pager = get_pager('search_hotel.php', $pager['search'], $count, $page, $size);
		$pager['display'] = $display;

		$smarty->assign('search_data_list', $search_data_list);
		$smarty->assign('pager', $pager);
		$smarty->assign('page_title', $hotel_inf['city_name'].$hotel_inf['area'].'搜索预订酒店');    // 页面标题
		// 当前位置
		$ur_here=array();
		$ur_here[]=array('txt'=>'酒店搜索预订','url'=>'?');
		if($hotel_inf['city_name']){$ur_here[]=array('txt'=>$hotel_inf['city_name'],'url'=>'?city_id='.$hotel_inf['city_id']);}
		if($hotel_inf['area']){$ur_here[]=array('txt'=>$hotel_inf['area'],'url'=>'?area_id='.$hotel_inf['area_id']);}
		if($hotel_inf['biz_name']){$ur_here[]=array('txt'=>$hotel_inf['biz_name'],'url'=>'?area_id='.$hotel_inf['biz_area_id']);}
		$ur_here[]=array('txt'=>'五星酒店','url'=>'?hotel_star=5');
		$ur_here[]=array('txt'=>'四星酒店','url'=>'?hotel_star=4');
		$ur_here[]=array('txt'=>'三星酒店','url'=>'?hotel_star=3');
		$ur_here[]=array('txt'=>'二星酒店','url'=>'?hotel_star=2');
		$ur_here[]=array('txt'=>'一星酒店','url'=>'?hotel_star=1');
		$ur_here[]=$smarty->assign('ur_here',get_url_here($ur_here));  // 当前位置
		//搜索到的酒店的房型
		$room_srh=array();
		$hotel_arr=array();
		if(!empty($search_data_list)){
			foreach ($search_data_list as $key=>$val){
				$hotel_arr[]=$val['goods_id'];
			}
		}
		$room_srh['hotel_id_in']=implode(',',$hotel_arr);
		if(!empty($room_srh['hotel_id_in'])){
			$room_kind_list_db=get_room_kind_list($room_srh);
			if(!empty($room_kind_list_db)){
				foreach ($room_kind_list_db as $key=>$val){
					if(count($room_kind_list[$val['hotel_id']])<3){
						$room_kind_list[$val['hotel_id']][]=$val;
					}
				}
			}
		}//print_pre_r($room_kind_list_db);
		$smarty->assign('room_kind_list',$room_kind_list); 

		//本市行政区列表
		$here_area_list=get_area($hotel_inf['city_id']);
		$smarty->assign('here_area_list',$here_area_list); 
		//本市商业区列表
		$biz_srh=array();
		$biz_srh['city_id']=$hotel_inf['city_id'];
		$biz_zone_list=get_biz_zone_list($biz_srh);
		$smarty->assign('biz_zone_list',$biz_zone_list); 
		//周边城市
		$here_city_list=array();
		if(!empty($hotel_city_list)){
			foreach ($hotel_city_list as $key=>$val){
				if($val['father_id']==$hotel_inf['father_id']){
					$here_city_list[]=$val;
				}
			}
		}
		$smarty->assign('here_city_list',$here_city_list);
		//本市推荐酒店
		$push_param=array();
		$push_param['city_id']=$hotel_inf['city_id'];
		$push_param['is_hot']=1;
		$hotel_push_list=get_hotel_list($push_param,20,1,$sort,$order);//执行搜索
		$smarty->assign('hotel_push_list',$hotel_push_list['list']); 
//print_pre_r($hotel_push_list);	
		//判断是否生成缓存文件
		if(count($srch_param)==1&&(!empty($srch_param['city_name_cn'])||!empty($srch_param['city_id'])||!empty($srch_param['area_id'])
		||!empty($srch_param['hotel_star'])||!empty($srch_param['city_name']))){//一般的搜索不生成缓存文件
			$cache_id='';//不生成缓存文件
		}
		$smarty->display('hotel_list.dwt',$cache_id);
	}
}









/**
 * 酒店列表-未使用
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function get_hotel_list_srh($srch_param, $size, $page, $sort, $order)
{
	global $db,$ecs,$as;
    if(empty($srch_param)){return array();}
    extract($srch_param);
    
    $where = " where 1 and cat_id='".HOTEL_CAI_ID."' ";

    
    if (!empty($city_name)){$where.=" and city_name like '%".$city_name."%'";}
    if (!empty($city_name_cn)){$where.=" and city_name_cn='".$city_name_cn."'";}
    if (!empty($city_name_cn_like)){$where.=" and city_name_cn like '%".$city_name_cn_like."%'";}
    if (!empty($goods_name)){$where.=" and goods_name like '%".$goods_name."%'";}
    if (!empty($hotel_name)){$where.=" and goods_name like '%".$hotel_name."%'";}
    if (!empty($serve_item)){$where.=" and serve_item like '%".$serve_item."%'";}
    if (!empty($food_establishment)){$where.=" and food_establishment like '%".$food_establishment."%'";}
    if (!empty($establishment)){$where.=" and establishment like '%".$establishment."%'";}
    if (!empty($credit_card)){$where.=" and credit_card like '%".$credit_card."%'";}
    if (!empty($position_txt)){$where.=" and (near_txt like '%".$position_txt."%' or position_txt like '%".$position_txt."%')";}
    if (!empty($near_txt)){$where.=" and (near_txt like '%".$near_txt."%' or position_txt like '%".$near_txt."%')";}
    if (!empty($is_on_sale_txt)){
    	if($is_on_sale_txt=='all'){
    		$where.=" and is_on_sale_txt <>'关闭'";
    	}else{
    		$where.=" and is_on_sale_txt ='".$is_on_sale_txt."'";
    	}
    }else{$where.=" and (is_on_sale_txt <>'关闭' or is_on_sale_txt is NULL)";}
    if (!empty($english_name)){$where.=" and (spell_cn like '%".$english_name."%' or english_name like '%".$english_name."%')";}
    if (!empty($spell_cn)){$where.=" and (spell_cn like '%".$spell_cn."%' or english_name like '%".$spell_cn."%')";}
    if (!empty($bank_suport)){$where.=" and bank_suport like '%".$bank_suport."%'";}
    if (!empty($is_hot)){$where.=" and is_hot='".$is_hot."'";}
    if (!empty($nation_id)){$where.=" and nation_id='".$nation_id."'";}
    if (!empty($province_id)){$where.=" and province_id='".$province_id."'";}
    if (!empty($biz_area_id)){$where.=" and biz_area_id='".$biz_area_id."'";}
    if (!empty($area_id)){$where.=" and area_id='".$area_id."'";}
    if (!empty($city_id)){$where.=" and city_id='".$city_id."'";}
    if (!empty($hc_id)){$where.=" and hc_id='".$hc_id."'";}
    if (!empty($hotel_star)){$where.=" and hotel_star='".$hotel_star."'";}
    if (!empty($hotel_star_1)){$where.=" and hotel_star>='".$hotel_star_1."'";}
    if (!empty($hotel_star_2)){$where.=" and hotel_star<='".$hotel_star_2."'";}
    if (!empty($multiple_id)){$where.=" and multiple_id='".$multiple_id."'";}
    if (!empty($room_price_min)){$where.=" and room_price_min>='".$room_price_min."'";}
    if (!empty($room_price_max)){$where.=" and room_price_max<='".$room_price_max."'";}

    $sql='';
    $sql = 'select count(*) as num FROM ' . $ecs->table('goods') . ' AS g ' .
            'LEFT JOIN (select hc_id as hc_id_hc,city_name,city_name_cn,hc_id_from,father_id from ' . $as->table('hotel_city') . ") AS hc ON hc.hc_id_hc = g.hc_id " .
            'LEFT JOIN (select areaID,area from ' . $as->table('area') . ") AS ar ON ar.areaID = g.area_id " .
            'LEFT JOIN (select biz_id,biz_name from ' . $as->table('biz_zone') . ") AS bz ON bz.biz_id = g.biz_area_id " .
            " $where";
    $count=$db->getOne($sql);//总记录数
    /* 获得商品列表 */
    $sql = 'SELECT * FROM ' . $ecs->table('goods') . ' AS g ' .
            'LEFT JOIN (select hc_id as hc_id_hc,city_name,city_name_cn,hc_id_from,father_id from ' . $as->table('hotel_city') . ") AS hc ON hc.hc_id_hc = g.hc_id " .
            'LEFT JOIN (select areaID,area from ' . $as->table('area') . ") AS ar ON ar.areaID = g.area_id " .
            'LEFT JOIN (select biz_id,biz_name from ' . $as->table('biz_zone') . ") AS bz ON bz.biz_id = g.biz_area_id " .
            " $where  ORDER BY $sort $order ";
    $res = $db->selectLimit($sql, $size, ($page - 1) * $size);
    
    $arr = array();
    $i=0;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
    	$row['key']=++$i;
        $arr['list'][]=$row;
        
    }
    $arr['count']=$count;
    return $arr;
}

?>